package tk.jfree.example.es.api.service;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;

import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import tk.jfree.example.es.api.pagehelper.Page;
import tk.jfree.example.es.model.Article;
import tk.jfree.example.es.model.Query;

import java.util.Optional;

/**
 * @author Cheng.Wei
 */
@Slf4j
@Service
@AllArgsConstructor
public class ArticleQueryRepositoryImpl implements ArticleQueryRepository{

    private final ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public Page query(Query query) {
        log.debug("查询{}", query);
        //构造查询条件  multiMatchQuery(query.getKeyWord(), "title", "author", "content")
//        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
//                .should(QueryBuilders.matchQuery("title", query.getKeyWord()))
//                .should(QueryBuilders.matchQuery("author", query.getKeyWord()))
//                .should(QueryBuilders.matchQuery("content", query.getKeyWord()));
        QueryBuilder builder = QueryBuilders.simpleQueryStringQuery(query.getKeyWord());
        //高亮样式
        String prefix = "<em style='color:red'>";
        String postfix = "</em>";
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(builder)
                //分页
                .withPageable(
                        PageRequest.of(
                                Optional.ofNullable(query.getPageNum()).orElse(1)-1,
                                Optional.ofNullable(query.getPageSize()).orElse(20))
                )
                //设置高亮域{待调整}
                .withHighlightFields(
                        new HighlightBuilder.Field("title").preTags(prefix).postTags(postfix).fragmentOffset(100).forceSource(true),
                        new HighlightBuilder.Field("author").preTags(prefix).postTags(postfix).fragmentOffset(100).forceSource(true),
                        new HighlightBuilder.Field("content").preTags(prefix).postTags(postfix).fragmentOffset(100).forceSource(true))
                .build();


        AggregatedPage<Article>  aggregatedPage =  elasticsearchTemplate.queryForPage(searchQuery, Article.class, new ArticleResultMapper());
        log.debug("查询结果{}", aggregatedPage);
        Page page = Page.builder()
                .pageNum(query.getPageNum())
                .pageSize(query.getPageSize())
                .total(aggregatedPage.getTotalElements())
                .pages(aggregatedPage.getTotalPages())
                .list(aggregatedPage.getContent())
                .build();
        return page;
    }
}
